/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.asset.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.asset.model.Asset;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class AssetSqlImpl {

    private static final String NAMESPACE = "psncard.";

    public static List<Asset> psnAssets(Map map) {
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        return IbatisTemplate.queryForList(NAMESPACE + "psnAssetCard", map);
    }

    public static List<Asset> psnAssets_askForli(Map map) {
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        return IbatisTemplate.queryForList(NAMESPACE + "psnAssetCard_askForList", map);
    }

    public static List<Asset> psnAssetCardForAudit(Map map) {
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        return IbatisTemplate.queryForList(NAMESPACE + "psnAssetCardForAudit", map);
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("orgCode", "10201");
        map.put("cpsn_number", "01004");
        map.put("checkFlag", "1");
        List<Asset> li = psnAssets_askForli(map);
        System.out.println("li size : " + li.size());
        for (Asset row : li) {
            System.out.println("row : " + row.getAssetCode() + " -- " + row.isCheckFlag());
        }
    }

    public static void updateAssetUseStatus(Asset ast) {
        IbatisTemplate.update(NAMESPACE + "updateAssetUseStatus", ast);
    }

    public static void batchUpdateAssetUseStatus(List<Asset> cards) {
        SqlMapClient sqlMapClient = IbatisTemplate.getSqlMapClient();
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.startBatch();
            for (Asset ast : cards) {
                sqlMapClient.update(NAMESPACE + "updateAssetUseStatus", ast);
            }
            sqlMapClient.executeBatch();
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void batchAuditPsnAsset(List<Asset> cards) {
        SqlMapClient sqlMapClient = IbatisTemplate.getSqlMapClient();
        try {
            sqlMapClient.startTransaction();
            sqlMapClient.startBatch();
            for (Asset ast : cards) {
                sqlMapClient.update(NAMESPACE + "auditPsnAsset", ast);
            }
            sqlMapClient.executeBatch();
            sqlMapClient.commitTransaction();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static List<Asset> psnAssetsByStatus(Map map) {
        return IbatisTemplate.queryForList(NAMESPACE + "psnAssetsByStatus", map);
    }

    public static void insertli(List<Asset> datali) {
        IbatisTemplate.insertList(NAMESPACE + "insertAsset", datali);
    }
}
